#include "cuda_runtime.h"
#include "device_launch_parameters.h"

#include <System.cu\cuPrintf.cu>
#include <System.cu\cuFalloc.cu>
#include <System\TreeSet.h>

using namespace System;
template class TreeSet<int>;

__device__ int TreeSet_COMPARE(unsigned __int32 shard, void* x, void* y)
{
	int a = *((int*)x);
	int b = *((int*)y);
    return (a < b ? -1 : (a > b ? 1 : 0));
}

__global__ void testTreeSet(fallocHeap* heap)
{
	fallocInit(heap);
	fallocContext* ctx = fallocCreateCtx(heap);
	fallocContext* stack = fallocCreateCtx(heap);
	falloc(stack, 70, false);

	//
	TreeSet<int> treeSet; treeSet.xtor(0, ctx);
	treeSet.Add(5);
	treeSet.Add(3);
	treeSet.Add(1);
	treeSet.Add(2);
	treeSet.Add(7);
	treeSet.Add(10);

	//
	TreeSet<int>::Enumerator e; treeSet.GetEnumerator(e, stack);
	while (e.MoveNext(&treeSet))
		cuPrintf("%d\n", e.Current);
	e.Dispose();

	//
	fallocDisposeCtx(stack);
	fallocDisposeCtx(ctx);
}

int main()
{
	cudaFallocHost fallocHost = cudaFallocInit(2048);
	cudaPrintfInit(256000);

	// test
	testTreeSet<<<1, 1>>>(fallocHost.heap);

	// free and exit
	cudaPrintfDisplay(stdout, true); cudaPrintfEnd();
	cudaFallocEnd(fallocHost);
	printf("\ndone.\n"); //char c; scanf_s("%c", &c);
    return 0;
}
